<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit();
}

/**
 * Mileage
**/
class Mileage_mileBlock extends Legacy_BlockProcedure
{
	/**
	 * @var Mileage_MileHandler
	 * 
	 * @private
	**/
	var $_mHandlerMile = null;

	/**
	 * @var Mileage_RecHandler
	 * 
	 * @private
	**/
	var $_mHandlerRec = null;

	/**
	 * @var Mileage_MileObject
	 * 
	 * @private
	**/
	var $_mObjectMile = null;

	/**
	 * @var string[]
	 * 
	 * @private
	**/
	var $_mOptions = array();

	/**
	 * @var object
	 * 
	 * @private
	**/
	var $_actionForm = NULL;

	/**
	 * prepare
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @public
	**/
	function prepare()
	{
		return parent::prepare() && $this->_parseOptions() && $this->_setupObject();
	}
	
	/**
	 * _parseOptions
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @private
	**/
	function _parseOptions()
	{
		$opts = explode('|', $this->_mBlock->get('options'));
		$this->_mOptions = array(
			
		);
		
		return true;
	}
	
	/**
	 * getBlockOption
	 * 
	 * @param   string  $key
	 * 
	 * @return  string
	 * 
	 * @public
	**/
	function getBlockOption($key)
	{
		return isset($this->_mOptions[$key]) ? $this->_mOptions[$key] : null;
	}
	
	/**
	 * getOptionForm
	 * 
	 * @param   void
	 * 
	 * @return  string
	 * 
	 * @public
	**/
	function getOptionForm()
	{
		if(!$this->prepare())
		{
			return null;
		}
	}
	
	/**
	 * _setupObject
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @private
	**/
	function _setupObject()
	{
		$root =& XCube_Root::getSingleton();
		$uid = 0;
		if (is_object($root->mContext->mXoopsUser)) {
			$uid = $root->mContext->mXoopsUser->get('uid');
		}

		$dirname = $this->_mBlock->get('dirname');

		//get module asset for handlers
		$asset = null;
		XCube_DelegateUtils::call(
			'Module.mileage.Global.Event.GetAssetManager',
			new XCube_Ref($asset),
			$dirname
		);

		// GET access uri (current uri)
		$this->access_uri = Mileage_Utils::getAccessUri();

		// MILE
		$this->_mHandlerMile =& $asset->getObject('handler', 'mile');

		// 完全一致
		$mileCriteria = new CriteriaCompo('1', '1');
		$mileCriteria->add(new Criteria('url', $this->access_uri));
		$objectMile =& $this->_mHandlerMile->getObjects($mileCriteria);

		if (count($objectMile) == 0) {

			// 部分一致
			$mileCriteria2 = new CriteriaCompo('1', '1');
			$mileCriteria2->add(new Criteria('suburl', 1));
			$objectMile2 = & $this->_mHandlerMile->getObjects($mileCriteria2);
			for ($i = 0; $i < count($objectMile2); $i++) {
				$reg_mile_url = $objectMile2[$i]->get('url');
				// 先頭一致
				if (strpos($this->access_uri, $reg_mile_url) === 0) {
					$objectMile[0] = $objectMile2[$i];
					break;
				}
				// 分解一致
				$p_url = parse_url($reg_mile_url);
				$base_url = $p_url['scheme'] . '://' . $p_url['host'] . $p_url['path'];
				$base_url = str_replace(array('index.php', 'index.html'), '', $base_url);

				if (strpos($this->access_uri, $base_url) !== 0) {
					continue;
				}
				if (empty($p_url['query'])) {
					$objectMile[0] = $objectMile2[$i];
					break;
				}
				$q = explode('&', $p_url['query']);
				$count = count($q);
				$check_count = 0;
				for ($j = 0; $j < $count; $j++) {
					if (strpos($this->access_uri, $q[$j]) > 0) {
						$check_count++;
					}
				}

				if ($count === $check_count) {
					$objectMile[0] = $objectMile2[$i];
					break;
				}
			}
		}

		// REC
		$_mHandlerRec =& $asset->getObject('handler', 'rec');

		$can_get_point = FALSE;
		if (count($objectMile) > 0) {
			foreach ($objectMile as $key => $obj) {
				$point = $obj->get('point');
				if ($point < 0 && empty($uid)) {
					$root->mController->executeRedirect(XOOPS_URL . '/', 2, _NOPERM);
				}

				$mile_id = $obj->get('mile_id');
				$interval = $obj->get('interval');

				if (Mileage_Utils::notExistsMileIDinRec($_mHandlerRec, $mile_id, $interval, $uid, $dirname)) {
					if ($point < 0) {
						// TO history of mileage
						$forwardUri = mileage_make_uri($dirname, 'SaleList');
						$_SESSION['MILEAGE_BUY_POINT_MSG'] = _MB_MILEAGE_BUY_PAGE_BY_POINT;
						$_SESSION['MILEAGE_BUY_POINT_MILE_ID'] = $mile_id;
						$root->mController->executeForward($forwardUri);
						exit;
					}
				//	$can_get_point = TRUE;
					$objectMile[$key]->canGetPoint = TRUE;
				}
			}
		}

		if (empty($uid)) {
			return FALSE;
		}

	//	if ($can_get_point === TRUE) {
			$ActionForm =& $asset->getObject('form', 'rec', FALSE, 'edit');
	//	}

		$this->_mObjectMile = $objectMile;
	//	$this->_can_get_point = $can_get_point;
		if (!empty($ActionForm)) {
			$this->_actionForm = $ActionForm;
		}

		return TRUE;
	}

	/**
	 * execute
	 * 
	 * @param   void
	 * 
	 * @return  void
	 * 
	 * @public
	**/
	function execute()
	{
		$root =& XCube_Root::getSingleton();

		$dirname = $this->_mBlock->get('dirname');

		$render =& $this->getRenderTarget();
		$render->setTemplateName($this->_mBlock->get('template'));

		if (count($this->_mObjectMile) > 0) {
			$render->setAttribute('miles', $this->_mObjectMile);
		}
		$render->setAttribute('dirname', $dirname);
		$render->setAttribute('lang', Mileage_Utils::getConstantLang($dirname));
		$render->setAttribute('totalPoint', Mileage_Utils::totalMileByUid($dirname));
		$render->setAttribute('access_uri', $this->access_uri);
		$render->setAttribute('actionForm', $this->_actionForm);
		$render->setAttribute('isMobile', Mileage_Utils::checkMobile());

		$renderSystem =& $root->getRenderSystem($this->getRenderSystemName());
		$renderSystem->renderBlock($render);
	}

}

